# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=geoipupdate
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.0.4
pkgrel=1
pkgdesc='Update GeoIP2 and GeoIP Legacy binary databases from MaxMind (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://dev.maxmind.com/geoip/geoipupdate"
license=('spdx:Apache-2.0 OR spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-go" "git")
backup=("${MINGW_PREFIX:1}/etc/GeoIP.conf")
options=('!strip')
source=("https://github.com/maxmind/geoipupdate/archive/v$pkgver/$_realname-$pkgver.tar.gz"
        "001-relocate.patch"
        "002-change-defaults-dirs.patch")
sha256sums=('02bc5dd121dfe232bf023bd0f736ddb05ba55061034e73d6036e940b34995844'
            'e0bb4ea6db98acafc118abd98d6857c2de0d45b5da91b4051f98581a8cf2539d'
            '7a8085a30d3f7e7dc166720050ddc496b11e447766c048a0a3be01936f6426f8')

prepare() {
  cd "$srcdir/$_realname-$pkgver"

  sed -i 's/EditionIDs .*/EditionIDs GeoLite2-ASN GeoLite2-City GeoLite2-Country/' conf/GeoIP.conf.default

  patch -Nbp1 -i "${srcdir}/001-relocate.patch"
  patch -Nbp1 -i "${srcdir}/002-change-defaults-dirs.patch"
}

build() {
    cd "$srcdir/$_realname-$pkgver"

    export GOROOT=${MINGW_PREFIX}/lib/go

    cd cmd/geoipupdate
    mkdir -p "$srcdir/build"
    go build  -o "$srcdir/build/geoipupdate.exe" -ldflags "-X main.version=$pkgver"
}

package() {
    cd "$srcdir/$_realname-$pkgver"

    install -dm755 "${pkgdir}${MINGW_PREFIX}/var/lib/GeoIP"
    install -Dm600 conf/GeoIP.conf.default "${pkgdir}${MINGW_PREFIX}/etc/GeoIP.conf"

    cd "$srcdir/build"
    install -Dm755 geoipupdate.exe "${pkgdir}${MINGW_PREFIX}/bin/geoipupdate.exe"

    cd "$srcdir/$_realname-$pkgver"
    install -Dm644 LICENSE-APACHE "${pkgdir}${MINGW_PREFIX}/usr/share/licenses/$_realname/LICENSE-APACHE"
    install -Dm644 LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/usr/share/licenses/$_realname/LICENSE-MIT"
}
